
rm(list = ls())
set.seed(1234567)
# Clear memory ------------------------------------------------------------


# Load data ---------------------------------------------------------------

source("03_code/00_data_loading/00_load_data_and_packages.R")

# Build codebooks ---------------------------------------------------------
# Only for data that are not labelled and have been collected using surveyCTO

source("03_code/00_data_loading/01_build_codebooks.R")

# Building data for analysis ----------------------------------------------

rerun_data_building <- TRUE

if(rerun_data_building){
  

# Clean data --------------------------------------------------------------

source("03_code/00_data_loading/01_clean_uganda1.R")
source("03_code/00_data_loading/01_clean_uganda2.R")
source("03_code/00_data_loading/01_clean_uganda3.R")
source("03_code/00_data_loading/01_clean_tanzania3.R")

# Load useful functions ---------------------------------------------------

source("03_code/01_useful_functions/variable_building_functions.R")
source("03_code/01_useful_functions/imputation_functions.R")

# Code variables ----------------------------------------------------------

# Uganda 
source("03_code/02_variable_coding/01_Uganda1.R")
source("03_code/02_variable_coding/01_Uganda2.R")
source("03_code/02_variable_coding/01_Uganda3.R")

# Tanzania
source("03_code/02_variable_coding/02_Tanzania1.R")
source("03_code/02_variable_coding/02_Tanzania2.R")
source("03_code/02_variable_coding/02_Tanzania3.R")

# South Africa
source("03_code/02_variable_coding/03_South_Africa.R")

# Afrobarometer 
source("03_code/02_variable_coding/04_Afrobarometer.R")

# Impute variables --------------------------------------------------------

source("03_code/03_imputations/01_Uganda_imputations.R")
source("03_code/03_imputations/02_Tanzania_imputations.R")
source("03_code/03_imputations/03_South_Africa_imputations.R")
source("03_code/03_imputations/04_Afrobarometer_imputations.R")

# Combine datasets --------------------------------------------------------

source("03_code/04_merging/01_make_full_data.R")
source("03_code/04_merging/02_make_full_data_no_imputations.R")

  # Save data image ---------------------------------------------------------
  
  save.image(file = "01_data/all_data.Rdata")
  
} else {
  
  load(file = "01_data/all_data.Rdata")
  
}


# Analyses ----------------------------------------------------------------

# Main text
# Table 1
source("03_code/05_analyses/00_main_text/main_gender_result.R")
# Table 2 and 3
source("03_code/05_analyses/00_main_text/plausibility_vignette_experiment.R")
# Table 4
source("03_code/05_analyses/00_main_text/fear_false_accusations.R")


# Appendix
# Figure 1
source("03_code/05_analyses/01_appendix/prisons.R")
# Tables A1 and A2
source("03_code/05_analyses/01_appendix/plausibility_vignette_experiment_black_magic.R")
# Table A3
source("03_code/05_analyses/01_appendix/views_on_police.R")
# Table A4
source("03_code/05_analyses/01_appendix/justice_preferences.R")
# Figure 2
source("03_code/05_analyses/01_appendix/age.R")
# Tables A5 and A6
source("03_code/05_analyses/01_appendix/plausibility_vignette_experiment_full_outcome.R")
# Table A7
source("03_code/05_analyses/01_appendix/main_gender_result_no_imp.R")
# Tables A8 and A9
source("03_code/05_analyses/01_appendix/plausibility_vignette_experiment_no_imp.R")
# Table A10
source("03_code/05_analyses/01_appendix/fear_false_accusations_no_imp.R")
# Table A11
source("03_code/05_analyses/01_appendix/views_on_police_no_imp.R")
# Table A12
source("03_code/05_analyses/01_appendix/justice_preferences_no_imp.R")
